package com.ruoyi.project.NH.average_income_comparison.service.impl;

import java.util.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.NH.average_income_comparison.mapper.AverageIncomeInfoMapper;
import com.ruoyi.project.NH.average_income_comparison.domain.AverageIncomeInfo;
import com.ruoyi.project.NH.average_income_comparison.service.IAverageIncomeInfoService;

/**
 * 户收入对比Service业务层处理
 * 
 * @author 狗熊岭
 * @date 2023-08-05
 */
@Service
public class AverageIncomeInfoServiceImpl implements IAverageIncomeInfoService 
{
    @Autowired
    private AverageIncomeInfoMapper averageIncomeInfoMapper;

    /**
     * 查询户收入对比
     * 
     * @param fid 户收入对比主键
     * @return 户收入对比
     */
    @Override
    public AverageIncomeInfo selectAverageIncomeInfoByFid(String fid)
    {
        return averageIncomeInfoMapper.selectAverageIncomeInfoByFid(fid);
    }

    /**
     * 查询户收入对比列表
     * 
     * @param averageIncomeInfo 户收入对比
     * @return 户收入对比
     */
    @Override
    public List<AverageIncomeInfo> selectAverageIncomeInfoList(AverageIncomeInfo averageIncomeInfo)
    {
        return averageIncomeInfoMapper.selectAverageIncomeInfoList(averageIncomeInfo);
    }


    /**
     * 仿写c++数对，不会写就不要瞎改！
     * @param <L>
     * @param <R>
     */
    private class Pair<L, R>{
        private L left;
        private R right;

        public L getLeft() {
            return left;
        }

        public void setLeft(L left) {
            this.left = left;
        }

        public R getRight() {
            return right;
        }

        public void setRight(R right) {
            this.right = right;
        }

        Pair(L left, R right){
            this.left = left;
            this.right = right;
        }
        Pair(){}
    }

    /**
     * 查找返贫算法
     */
    public List<AverageIncomeInfo> findBackIntoPoverty(AverageIncomeInfo averageIncomeInfo){
        Integer maxYear = averageIncomeInfoMapper.getMaxReportYear();
        //如果数据全部封停，则调用系统时间
        if(maxYear == null){
            Calendar cal = Calendar.getInstance();
            maxYear = cal.get(Calendar.YEAR);
        }
        AverageIncomeInfo tmp = new AverageIncomeInfo(averageIncomeInfo);
        tmp.setReportYear(maxYear);
        List<AverageIncomeInfo> thisYearList = selectAverageIncomeInfoList(tmp);
        tmp.setReportYear(maxYear - 1);
        List<AverageIncomeInfo> lastYearList = selectAverageIncomeInfoList(tmp);
        List<AverageIncomeInfo> result = new ArrayList<>(Collections.emptyList());
        HashMap<String, Pair<AverageIncomeInfo, Double>> hashMap = new HashMap<>();
        //将今年收入传入数对右值
        for(AverageIncomeInfo i:thisYearList){
//            i.setIsBackIntoPoverty(0);
            hashMap.put(i.getFid(), new Pair<>(i, i.getAvgIncome()));
        }
        //将数对右值与去年收入相减并作判断
        for(AverageIncomeInfo i:lastYearList){
            Pair<AverageIncomeInfo, Double> pair = hashMap.get(i.getFid());
            hashMap.remove(i.getFid());
            tmp = pair.left;
            tmp.setLastAvgIncome(i.getAvgIncome());
            if (pair.getRight() < i.getAvgIncome()) {
                tmp.setIsBackIntoPoverty(1);
            }
//            else {
//                tmp.setIsBackIntoPoverty(0);
//            }
            result.add(tmp);
        }
        //如果没有去年的信息，但是有今年的信息，依然传入结果中
        if(!hashMap.isEmpty()){
            for(String key:hashMap.keySet()){
                Pair<AverageIncomeInfo, Double> pair = hashMap.get(key);
                result.add(pair.left);
            }
        }
        //排序，让返贫的始终在上面
        //result.sort((l, r) -> r.getIsBackIntoPoverty().compareTo(l.getIsBackIntoPoverty()));
        result.sort((l, r) -> Objects.equals(l.getIsBackIntoPoverty(), r.getIsBackIntoPoverty()) ? l.getFid().compareTo(r.getFid()) : r.getIsBackIntoPoverty() - l.getIsBackIntoPoverty());
        return result;
    }

}
